<?php

namespace app\store\controller\join;

use app\common\model\Areas;
use app\common\model\Joins;
use app\store\controller\Controller;
use app\store\model\Setting as SettingModel;
use app\store\model\user\PointsLog as PointsLogModel;

/**
 * 积分管理
 * Class Points
 * @package app\store\controller\market
 */
class Address extends Controller
{

    /**
     * 首页
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 积分明细列表
        $model = new Areas();
        $list = $model->getList($this->request->param());
        return $this->fetch('index', compact('list'));
    }

    /**
     * 加盟人员
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function joins()
    {
        // 积分明细列表
        $model = new Joins();
        $list = $model->getList($this->request->param());
        return $this->fetch('joins', compact('list'));
    }

    /**
     * 添加
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new Areas;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        $data = $this->postData('address');
        $data['wxapp_id'] = self::getWxappId();
        $model->create($data);
        return $this->renderSuccess('添加成功', url('join.address/index'.'&type='.$data['type'].'&pid='.$data['pid']));
    }

    /**
     * 导出加盟列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function export()
    {
        $exportList = Joins::order('id desc')->select();
        $titles = [
            '姓名', '加盟品牌', '加盟地址', '身份证号', '身份证正面地址', '身份证反面地址', '手机号', '年龄', '订单号', '是否支付', '支付时间', '提交时间'
        ];
        // 导出csv文件
        $filename = '加盟申请-' . date('YmdHis');

        $datas = [];
        foreach ($exportList as $item) {
            $datas[] = [
                '姓名' => $item->name,
                '加盟品牌' => $item->Brand,
                '加盟地址' => $item->address,
                '身份证号' => $item->idcard,
                '身份证正面地址' => $item->idcard_file1,
                '身份证反面地址' => $item->idcard_file2,
                '手机号' => $item->phone,
                '年龄' => $item->age,
                '订单号' => $item->order_num,
                '是否支付' => $item->is_pay ? '是' : '否',
                '支付时间' => $item->pay_time,
                '提交时间' => $item->create_time,
            ];
        }

        export_excel($filename . '.csv', $titles, $datas);
    }


    /**
     * 编辑
     * @param $shop_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        // 门店详情
        $model = Areas::FIND($id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 新增记录
        if ($model->save($this->postData('address'))) {
            return $this->renderSuccess('更新成功', url('join.address/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除门店
     * @param $shop_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($shop_id)
    {
        // 门店详情
        $model = Areas::find($shop_id);
        if (!$model->delete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}